﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using mfXmlSerializer.TestObjects;
using System.IO;
using System.Threading;
using System.Globalization;

namespace mfXmlSerializer.Test
{
    [TestClass]
    public class DeserializeTests
    {
        [TestMethod]
        public void Deserialize_Valid_SimpleCompleteParameterObject()
        {
            TestSetUps.SetupDateTimeTests();

            var refXml = TestDataFactory.CreateSimpleCompleteParameterString();

            var serPara = new mfXmlTypedSerializer(typeof(Parameter));

            var curPara = serPara.Deserialize(refXml) as Parameter;

            CheckDeserializedSimpleParameterObject(curPara, created: new DateTime(2014, 1, 12, 13, 12, 00));

        }

        public static void CheckDeserializedSimpleParameterObject(Parameter curPara
            , DateTime created 
            , string version = "OST v1.0"
            , int number = 48
            , double floating = 48.48
            , enumTraceLevel traceLevel = enumTraceLevel.Debug
            , bool loggingActive = true)
        {
            Assert.AreEqual(version, curPara.Version);
            Assert.AreEqual(number, curPara.Number);
            Assert.AreEqual(floating, curPara.FloatNumber);
            Assert.AreEqual(created, curPara.Created);
            Assert.AreEqual(traceLevel, curPara.LogConfig.TraceLevel);
            Assert.AreEqual(loggingActive, curPara.LogConfig.FileLoggingActive);
        }

        [TestMethod]
        public void Deserialize_Valid_SimpleParameterObject_WithEmptyVersionElement()
        {
            TestSetUps.SetupDateTimeTests();

            var refXml = TestDataFactory.CreateParameterString_WithEmptyVersionElement();

            var serPara = new mfXmlTypedSerializer(typeof(Parameter));

            var curPara = serPara.Deserialize(refXml) as Parameter;

            CheckDeserializedSimpleParameterObject(curPara, created: new DateTime(2014, 1, 12, 13, 12, 00), version: null);
        }

        [TestMethod]
        public void Deserialize_Valid_SimpleParameterObject_WithMissingVersionElement()
        {
            TestSetUps.SetupDateTimeTests();

            var refXml = TestDataFactory.CreateParameterString_WithMissingVersionElement();

            var serPara = new mfXmlTypedSerializer(typeof(Parameter));

            var curPara = serPara.Deserialize(refXml) as Parameter;

            CheckDeserializedSimpleParameterObject(curPara, created: new DateTime(2014, 1, 12, 13, 12, 00), version: null);
        }

        [TestMethod]
        public void Deserialize_Valid_SimpleParameterObject_WithMissingLoggingElement()
        {
            TestSetUps.SetupDateTimeTests();

            var refXml = TestDataFactory.CreateParameterString_WithMissingLoggingElement();

            var serPara = new mfXmlTypedSerializer(typeof(Parameter));

            var curPara = serPara.Deserialize(refXml) as Parameter;

            CheckDeserializedSimpleParameterObject(curPara
                , created: new DateTime(2014, 1, 12, 13, 12, 00)
                , traceLevel: new Logging().TraceLevel
                , loggingActive: new Logging().FileLoggingActive);
        }

        [TestMethod]
        public void Deserialize_Valid_RSSSample_Complete()
        {
            TestSetUps.SetupDateTimeTests();

            var refRss = TestDataFactory.CreateRSSSample_Complete();
            var refXml = TestDataFactory.CreateRSSSampleString_Complete();

            var serRSS = new mfXmlTypedSerializer(typeof(rss));

            var curRSS = serRSS.Deserialize(refXml) as rss;

            CheckRSSSample_Complete(curRSS);

        }

        public static void CheckRSSSample_Complete(rss curRSS)
        {
            var refRss = TestDataFactory.CreateRSSSample_Complete();

            Assert.AreEqual(1, curRSS.channel.Count);
            Assert.AreEqual(4, curRSS.channel[0].item.Count);

            //channel checks (incomplete)
            Assert.AreEqual(refRss.channel[0].pubDate, curRSS.channel[0].pubDate);
            Assert.AreEqual(refRss.channel[0].title, curRSS.channel[0].title);
            Assert.AreEqual(refRss.channel[0].description, curRSS.channel[0].description);
            Assert.AreEqual(refRss.channel[0].link, curRSS.channel[0].link);

            //random item checks (incomplete)
            Assert.AreEqual(refRss.channel[0].item[0].guid, curRSS.channel[0].item[0].guid);
            Assert.AreEqual(refRss.channel[0].item[0].title, curRSS.channel[0].item[0].title);
            Assert.AreEqual(refRss.channel[0].item[0].pubDate, curRSS.channel[0].item[0].pubDate);

            //null expected
            Assert.AreEqual(refRss.channel[0].item[1].title, curRSS.channel[0].item[1].title);

            Assert.AreEqual(refRss.channel[0].item[2].guid, curRSS.channel[0].item[2].guid);

            Assert.AreEqual(refRss.channel[0].item[3].description, curRSS.channel[0].item[3].description);
            Assert.AreEqual(refRss.channel[0].item[3].pubDate, curRSS.channel[0].item[3].pubDate);
        }
    }
}
